//
//  ReplyTableViewCell.m
//  ITHome
//
//  Created by lanou3g on 15/7/17.
//  Copyright (c) 2015年 YAX. All rights reserved.
//

#import "ReplyTableViewCell.h"
#import "NSDate+Extend.h"

@implementation ReplyTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.avatar = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
        self.avatar.layer.masksToBounds = YES;
        self.avatar.layer.borderWidth = 0.2;
        self.avatar.layer.borderColor = [[UIColor lightGrayColor]CGColor];
        self.avatar.layer.cornerRadius = 40 / 7;
        [self.contentView addSubview:_avatar];
        
        self.nickName = [[UILabel alloc]initWithFrame:CGRectMake(self.avatar.origin.x + 40 + 5, 10, 200, 15)];
        self.nickName.font = [UIFont systemFontOfSize:12];
        self.nickName.textColor = [UIColor grayColor];
        [self.contentView addSubview:_nickName];
        
        self.body = [[UILabel alloc]initWithFrame:CGRectMake(self.nickName.origin.x, self.nickName.origin.y + 15 + 20, kScreenWidth - 20 - 5 - 40, 40)];
        self.body.numberOfLines = 0;
        self.body.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:_body];
    }
    return self;
}

- (void)setModel:(ReplyModel *)model
{
    NSString *picture = [model.user objectForKey:@"avatar"];
    NSString *name = [model.user objectForKey:@"nickName"];
    [self.avatar sd_setImageWithURL:[NSURL URLWithString:picture]];
    NSString *date = [NSDate dateWithStringFromat:model.created_at];
    if (name == nil)
    {
        self.nickName.text = [NSString stringWithFormat:@"匿名用户-%@",date];
    }
    else
    {
        self.nickName.text = [NSString stringWithFormat:@"%@-%@",name,date];
    }
    self.body.text = model.body;
    // 计算label frame
    CGRect bounds = [self.body.text boundingRectWithSize:CGSizeMake(kScreenWidth - 20 - 5 - 40, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil];
    self.body.frame = CGRectMake(self.nickName.origin.x, self.nickName.origin.y + 15 + 20, kScreenWidth - 20 - 5 - 40, bounds.size.height);
}

+ (CGFloat)heightForCell:(ReplyModel *)model
{
    CGRect bounds = [model.body boundingRectWithSize:CGSizeMake(kScreenWidth - 20 - 5 - 40, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil];
    return 45 + bounds.size.height + 10;
}



@end
